package com.itheima.controller;


import com.itheima.entity.Dept;
import com.itheima.entity.Result;
import com.itheima.service.DeptService;
import com.itheima.service.DeptServiceImpl;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
public class DeptController {
//    @RequestMapping("/depts")
//    public List<Dept> GetAll(){
//        InputStream input = this.getClass().getClassLoader().getResourceAsStream("dept.txt");
//        List<String> strings = IOUtils.readLines(input, "UTF-8");
//        List<Dept> depts = strings.stream().map((str)->{
//            String[] parts = str.split(",");
//            Integer id =  Integer.valueOf(parts[0]);
//            String name = parts[1];
//            LocalDateTime updateTime = LocalDateTime.parse(parts[2],DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//            return new Dept(id,name,updateTime);
//        }).toList();
//        return depts;
//    }
//@RequestMapping(value = "/depts",method = RequestMethod.GET)
//    private DeptService deptService = new DeptServiceImpl();
    @Autowired
    private DeptService deptService;
    @GetMapping("/depts")
    //public Result GetAll(){
    //    InputStream input = this.getClass().getClassLoader().getResourceAsStream("dept.txt");
    //    List<String> strings = IOUtils.readLines(input, "UTF-8");
    //    List<Dept> depts = strings.stream().map((str)->{
    //        String[] parts = str.split(",");
    //        Integer id =  Integer.valueOf(parts[0]);
    //        String name = parts[1];
    //        LocalDateTime updateTime = LocalDateTime.parse(parts[2],DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    //        return new Dept(id,name,updateTime);
    //    }).toList();
    //    return Result.success(depts);
    //}

    public Result list(){
        List<Dept> depts = deptService.list();
        return Result.success(depts);
    }
}
